package com.sgkusbcamdemo;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.gkusbsdk.SZUSBFile;
import com.gkusbsdk.SZUSBSDK;
import com.qitianyong.selfclass.CONSTANT;
import com.qitianyong.selfclass.GKDevice;

/**
 * Created by gy on 2017/4/24.
 */

public class ShowVideoDataDialog extends AlertDialog implements SZUSBSDK.CallBack, View.OnClickListener {

    private SZUSBFile gkusbFile;
    private Button pause;
    private Button resume;
    private TextView content;

    protected ShowVideoDataDialog(Context context, SZUSBFile gkusbFile) {
        super(context);
        this.gkusbFile = gkusbFile;
        initView();
    }

    private void initView() {
        View inflate = View.inflate(getContext(), R.layout.layout_play_video, null);
        setView(inflate);
        pause = (Button) inflate.findViewById(R.id.pause);
        resume = (Button) inflate.findViewById(R.id.resume);
        content = (TextView) inflate.findViewById(R.id.content);
        pause.setOnClickListener(this);
        resume.setOnClickListener(this);
    }

    public void setGkusbFile(SZUSBFile gkusbFile) {
        this.gkusbFile = gkusbFile;
    }

    @Override
    public void show() {
        super.show();
        GKDevice.getInstance().setCallback(this);
    }

    @Override
    public void dismiss() {
        GKDevice.getInstance().setCallback(null);
        GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_STOP_PLAYBACK, gkusbFile);
        //播放时 设备会停止录像 需要手动开启才可以开始录像
        // PublicMethod.sleep(500);
        //GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_OPEN_RECORD, null);

        super.dismiss();

    }

    @Override
    public void cb_stream(long hUSBDevice, byte[] data, final int length, final int width, final int height, final int time) {
        //注意回放数据的发送速度是大于播放速度的，如果需要按照正常速度25帧播放，需要在本地做缓存播放处理
        content.post(new Runnable() {
            @Override
            public void run() {
                content.setText("time=" + time + " length=" + length + " width=" + width + " height=" + height);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pause:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_PAUSE_PLAYBACK, gkusbFile);

                break;
            case R.id.resume:
                GKDevice.getInstance().sendCommand(CONSTANT.COMMAND.CMD_RESUME_PLAYBACK, gkusbFile);
                break;
        }
    }
}
